﻿#pragma once
#include <thread>
#include <functional>

#define MAX_AUDIO_BUF 4 
#define BUFFERNOTIFYSIZE 4096

typedef std::function<void(void* buffer, unsigned long size)> SoundCallBack;

class OutputDevice
{
public:
	OutputDevice();
	~OutputDevice();

	/** 打开 */
	bool open(void* winId, int inChannels = 2, int sampleRate = 44100, int bitsPerSample = 16);

	/** 关闭 */
	void close();

	/** 播放 */
	void start();

	/** 停止播放 */
	void stop();

	/** 设置回调 */
	void setSoundCallBack(SoundCallBack& callBack);

private:
	struct IDirectSound8* _ds = nullptr;
	struct IDirectSoundBuffer* _dsBuffer = nullptr;
	struct IDirectSoundBuffer8* _dsBuffer8 = nullptr;
	void* _event[MAX_AUDIO_BUF];
	
	SoundCallBack _soundCallBack;

	bool _isPlaying = false;
	std::thread* _playThead = nullptr;

	/** 播放线程 */
	void play();
};